<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Model\Bill;
use App\Model\CashLog;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RedisException;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use Util\Common;
use App\Controller\AbstractController;
use App\Model\Distributors;

class ProfitController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**提现记录
     * @param RequestInterface $request
     * @return ResponseInterface
     */
    public function outCash(RequestInterface $request):ResponseInterface{
        $page = $request->inputs(['page','limit','cash_type',
            'distributor_id','time', 'status','is_return']);
        $data['list'] = Db::table('cash_log as c')
            ->leftJoin('distributors as d', 'c.distributor_id','=','d.id')
            ->where(function ($query) use ($page) {
                if($page['distributor_id']){
                    $query->where('c.distributor_id',$page['distributor_id']);
                }
                if($page['cash_type']){
                    $query->where('c.cash_type',$page['cash_type']);
                }
                if($page['status']){
                    $query->where('c.status',$page['status']);
                }
                if($page['is_return'] != ''){
                    $query->where('c.is_return',$page['is_return']);
                }
                if (isset($page['time']) && is_array($page['time'])) {
                    $query->where('c.created_at', '>=', $page['time'][0]);
                    $query->where('c.created_at', '<=', $page['time'][1]);
                }
            })->orderBy( 'id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->select('c.*','d.name')
            ->get();
        $data['total'] = Db::table('cash_log as c')
            ->leftJoin('distributors as d', 'c.distributor_id','=','d.id')
            ->where(function ($query) use ($page) {
                if($page['distributor_id']){
                    $query->where('c.distributor_id',$page['distributor_id']);
                }
                if($page['status']){
                    $query->where('c.status',$page['status']);
                }
                if($page['cash_type']){
                    $query->where('c.cash_type',$page['cash_type']);
                }
                if($page['is_return'] != ''){
                    $query->where('c.is_return',$page['is_return']);
                }
                if (isset($page['time']) && is_array($page['time'])) {
                    $query->where('c.created_at', '>=', $page['time'][0]);
                    $query->where('c.created_at', '<=', $page['time'][1]);
                }
            })->count();
        return $this->success($data);
    }

    /**分润流水记录
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getList(RequestInterface $request):ResponseInterface
    {
        $page = $request->inputs(['page','limit','out_ord_id',
            'distributor_id','time','payment_type',
            'status']);
        $data['list'] = Db::table('bills as b')
            ->leftJoin('orders as o', 'b.target_id','=','o.id')
            ->leftJoin('distributors as d', 'b.account_id','=','d.id')
            ->where(function ($query) use ($page) {
//                if (array_key_exists('keyword',$page) && $page['keyword']) {
//                    $query->where('o.number', '%'.$page['keyword'] . '%');
//                $query->orWhere('o.out_ord_id', 'like', '%'.$page['keyword'].'%');
//                }
                $query->where('account_type','App\Model\Distributors');
                if($page['distributor_id']){
                    $query->where('b.account_id',$page['distributor_id']);
                }
                if($page['payment_type']){
                    $query->where('o.payment_type',$page['payment_type']);
                }
                if (isset($page['time']) && is_array($page['time'])) {
                    $query->where('o.created_at', '>=', $page['time'][0]);
                    $query->where('o.created_at', '<=', $page['time'][1]);
                }
            })->orderBy('b.id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->select('b.*', 'o.number as order_number','o.sn','o.channel_number','d.name')
            ->get();
        $data['total'] = Db::table('bills as b')
            ->leftJoin('orders as o', 'b.target_id','=','o.id')
            ->leftJoin('distributors as d', 'b.account_id','=','d.id')
            ->where(function ($query) use ($page) {
//                if (array_key_exists('keyword',$page) && $page['keyword']) {
//                    $query->where('o.number', '%'.$page['keyword'] . '%');
//                $query->orWhere('o.out_ord_id', 'like', '%'.$page['keyword'].'%');
//                }
                $query->where('account_type','App\Model\Distributors');
                if($page['distributor_id']){
                    $query->where('b.account_id',$page['distributor_id']);
                }
                if($page['payment_type']){
                    $query->where('o.payment_type',$page['payment_type']);
                }
                if (isset($page['time']) && is_array($page['time'])) {
                    $query->where('o.created_at', '>=', $page['time'][0]);
                    $query->where('o.created_at', '<=', $page['time'][1]);
                }
            })->count();
        return $this->success($data);
    }

}
